package com.orm.analysis.action;

import com.opensymphony.xwork2.ActionContext;
import com.orm.analysis.model.ContactInfo;
import com.orm.analysis.model.Customer;
import com.orm.analysis.model.Salesman;
import com.orm.analysis.service.GenericService;

import java.util.List;
import java.util.Map;


public class SalesmanAction extends FrontendAction  {

    private GenericService<Salesman> service;
    private GenericService<ContactInfo> contactInfoService;
    private Salesman salesman;
    private List<ContactInfo> contactInfoList;
    private List<Salesman> salesmans;


    public SalesmanAction(GenericService<Salesman> service, GenericService<ContactInfo> contactInfoService) {
        this.service = service;
        this.contactInfoService = contactInfoService;
    }

    public String getAllSalesmans() {
        salesmans = service.list(page, size);
        return SUCCESS;
    }

    public String getBulkSalesmans() {
                            try {
                                salesmans = service.findAllWithDepth("contactInfo");

                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            return SUCCESS;
                        }

    public String getAllSalesmansContactInfo() {
                            try {
                                salesmans = service.findAllWithDepth("contactInfo");

                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            return SUCCESS;
                        }

     public String list() {
         salesmans = service.list(page, size);
        return SUCCESS;
    }

    public String setUpForInsert(){
        prep();

    		return "success";
    	}

 public String setUpForUpdate(){
        prep();
    		if (salesman != null && salesman.getId() != null) {
                salesman = service.getById (salesman.getId());
    		}
    		return "success";
    	}


        public String insert(){



        	if(!validationSuccessful()){
        		return "input";
        	}else{
        		if (salesman.getId()== null) {
                    service.create(salesman);
        		}
        	}
        	return "success";
        }

    public String update(){



        	if(!validationSuccessful()){
        		return "input";
        	}else{

                    service.update(salesman);


        	}
        	return "success";
        }


    private void prep() {
        contactInfoList = contactInfoService.list(page, size);
		Map session = ActionContext.getContext().getSession();
		session.put("contactInfoList", contactInfoList);
	}





    public String delete() {
        service.delete(salesman.getId());
        return SUCCESS;
    }


    private boolean validationSuccessful(){
       	if(salesman.getFirstName()==null||salesman.getFirstName().trim().length()<1){
       		addActionMessage("FirstName is required");
       	}
       	if(salesman.getLastName()==null||salesman.getLastName().trim().length()<1){
       		addActionMessage("LastName is required");
       	}

       	if(this.hasActionMessages()){
       		return false;
       	}else{
       		return true;
       	}


       }

    public Salesman getSalesman() {
        return salesman;
    }

    public void setSalesman(Salesman salesman) {
        this.salesman = salesman;
    }

    public List<Salesman> getSalesmans() {
        return salesmans;
    }

    public void setSalesmans(List<Salesman> salesmans) {
        this.salesmans = salesmans;
    }
}
